using System;
using System.Collections.Generic;

namespace Karma.Framework.Core.Modules.Reports.Definition.Expression
{
    internal class BetweenExpression : ExpressionBase
    {

        public BetweenExpression(Criteria criteria)
            : base(criteria)
        {
            if (criteria.OperandValues == null)
            {
                throw new ArgumentException(string.Format("{0}. The values cant be null.", GetType().Name));
            }
            if (criteria.OperandValues.Count != 2)
            {
                throw new ArgumentException(string.Format(
                                                "{0}. The expression must be composed by 2 values but it has [{1}].",
                                                GetType().Name, criteria.OperandValues.Count));
            }
        }

        public override CompiledExpression Compile(Report report)
        {
            var lbound = GenerateIdentifier();
            var rbound = GenerateIdentifier();

            var exp = new CompiledExpression
                          {
                              Parameters = new Dictionary<string, object>()
                          };
            exp.Expression = string.Format("{0}.{1} beetwen :{2} and :{3}", GetIdentifier(report), _criteria.Expression, lbound, rbound);

            exp.Parameters.Add(lbound, Cast(_criteria.OperandValues[0]));
            exp.Parameters.Add(rbound, Cast(_criteria.OperandValues[1]));
            return exp;
        }
    }
}